package com.astrob.adapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.model.RoutePlanIndex;
import com.astrob.util.Utils;
import com.astrob.nhcmap.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class MyRoutePathItemAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<RoutePlanIndex> mItemDatas;
	private int mSelectPos = -1;
	private boolean mIsLoadPath = false;
	
	private PoiItemListener listener = null;
	public void setListener(PoiItemListener listener) {
		this.listener = listener;
	}

	public MyRoutePathItemAdapter(Context context) {
		this.context = context;
	}

	public void setIsLoadPath(boolean isLoadPath) {
		mIsLoadPath = isLoadPath;
	}
	
	public void setData(ArrayList<RoutePlanIndex> itemDatas) {
		mItemDatas = itemDatas;
	}

	public void setSelectPos(int nPos) {
		mSelectPos = nPos;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mItemDatas != null)
			return mItemDatas.size();
		else
			return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (mItemDatas != null && mItemDatas.size() > position)
			return mItemDatas.get(position);
		else 
			return null;
	}
	
	public RoutePlanIndex getData(int position) {
		RoutePlanIndex data = new RoutePlanIndex();
		if (mItemDatas != null && mItemDatas.size() > position && position >= 0) {
			data = mItemDatas.get(position);
		}
		return data;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		//如果未选择或list列表为空时直接跳出，避免NullPointException
        if (position < 0 || mItemDatas.size() <= 0 || position >= mItemDatas.size()) {
            return null;
        }

        Log.d("THZ", "MyRoutePathItemAdapter getView:" + position);
        
		ViewHolder holder;
		if (null == convertView) {
			convertView = LinearLayout.inflate(context,
					R.layout.adapter_myroutepath_listview_item, null);

			holder = new ViewHolder();

			holder.tv_name = (TextView) convertView
					.findViewById(R.id.tv_item_name);
			holder.tv_time = (TextView) convertView
					.findViewById(R.id.tv_item_time);
			holder.btn_edit = (Button)convertView.findViewById(R.id.btn_edit);
			holder.btn_delete = (Button)convertView.findViewById(R.id.btn_delete);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		RoutePlanIndex index = mItemDatas.get(position);
		if (index != null) {
			Date date = new Date(index.getTimeStamp());
			SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat t = new SimpleDateFormat("HH:mm:ss");
			holder.tv_name.setText(index.getName().trim());
			holder.tv_time.setText(d.format(date));
		}
		
		if (mIsLoadPath) {
			holder.btn_edit.setVisibility(View.GONE);
			holder.btn_delete.setVisibility(View.GONE);
		}
		
		holder.btn_edit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (Utils.isFastDoubleClick())
					return;
				if (listener != null)
					listener.onEditinfo(position);				
			}
		});
		
		holder.btn_delete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (Utils.isFastDoubleClick())
					return;
				if (listener != null)
					listener.onDelete(position);	
			}
		});
		
		return convertView;
	}
	
	public interface PoiItemListener {
		void onEditinfo(int pos);
		void onDelete(int pos);
	}

	static class ViewHolder {
		TextView tv_name;
		TextView tv_time;
		Button btn_edit;
		Button btn_delete;
	}
}
